package com.example.servlet;

import com.alibaba.fastjson.JSON;
import com.example.dao.FileInfoDaoImpl;
import com.example.model.FileInfo;
import com.example.service.FileInfoService;
import com.example.service.FileInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ListFileInfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * 查询文件的接口
     * 返回一个JSON
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json; charset=utf-8");
        FileInfoService fileInfoService = new FileInfoServiceImpl(new FileInfoDaoImpl());
        List<FileInfo> fileInfos = fileInfoService.listFileInfos();
        String fileInfosJson = JSON.toJSONString(fileInfos);
        PrintWriter out = response.getWriter();
        out.write(fileInfosJson);
    }
}
